package com.system.honour.service.impl;

import com.system.honour.beans.PageQuery;
import com.system.honour.beans.PageResult;
import com.system.honour.common.ResultDTO;
import com.system.honour.dto.RoleDTO;
import com.system.honour.entity.SysRole;
import com.system.honour.entity.SysRolePermission;
import com.system.honour.entity.SysRoleUser;
import com.system.honour.exception.CustomizeErrorCode;
import com.system.honour.mapper.SysRoleMapper;
import com.system.honour.mapper.SysRolePermissionMapper;
import com.system.honour.mapper.SysRoleUserMapper;
import com.system.honour.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
@Slf4j
@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private SysRolePermissionMapper rolePermissionMapper;
    @Autowired
    private SysRoleUserMapper roleUserMapper;

    @Override
    public List<SysRole> getAll() {
        return roleMapper.getAll();
    }

    @Override
    public PageResult getRoleByPage(PageQuery page) {
        Integer count = roleMapper.countAllRole();
        List<SysRole> roleList = null;
        if (count>0){
            roleList = roleMapper.getRoleByPage(page);
            return PageResult.<SysRole>builder().data(roleList).total(count).build();
        }
        return PageResult.<SysRole>builder().build();
    }

    @Override
    public ResultDTO save(RoleDTO roleDTO) {
        //1.先保存角色
        SysRole role = SysRole.builder()
                .name(roleDTO.getName())
                .description(roleDTO.getDescription()).build();
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        roleMapper.insertSelective(role);
        List<Integer> permissionIds = roleDTO.getPermissionList();
        //移除0，permission id是从1开始的
        //permissionIds.remove(0);
        //2.保存角色对应的所有权限
        if (!CollectionUtils.isEmpty(permissionIds)){
            for (int i = 0; i < permissionIds.size(); i++) {
                SysRolePermission rolePermission = new SysRolePermission(role.getId(),permissionIds.get(i));
                rolePermissionMapper.insertSelective(rolePermission);
            }
        }
        return ResultDTO.okOf();
    }

    @Override
    public ResultDTO update(RoleDTO roleDTO) {
        //检查是否有权限传入；没有不做修改，有就进行删除再传入
        //1.有权限传入
        if (roleDTO.getPermissionList()!=null&&roleDTO.getPermissionList().size()>0){
            rolePermissionMapper.deleteByRoleId(roleDTO.getId());
            for (int i = 0; i < roleDTO.getPermissionList().size(); i++) {
                SysRolePermission rolePermission = new SysRolePermission(roleDTO.getId(),roleDTO.getPermissionList().get(i));
                rolePermissionMapper.insertSelective(rolePermission);
            }
        }
        //角色修改带id
        SysRole role = SysRole.builder()
                .id(roleDTO.getId())
                .name(roleDTO.getName())
                .description(roleDTO.getDescription()).build();
        role.setUpdateTime(new Date());
        roleMapper.updateByPrimaryKeySelective(role);
        return ResultDTO.okOf();


    }

    @Override
    public PageResult getRoleByVague(String name, PageQuery page) {
        Integer count = roleMapper.countRoleByVague(name);
        List<SysRole> roleList = null;
        if (count>0){
            roleList = roleMapper.getRoleByVague(page,name);
            return PageResult.<SysRole>builder().data(roleList).total(count).build();
        }
        return PageResult.<SysRole>builder().build();
    }

    @Override
    public ResultDTO deleteById(Integer roleId) {
        //查询角色是否存在
        SysRole role = roleMapper.selectByPrimaryKey(roleId);
        if (role!=null){
            //删除角色
            roleMapper.deleteByPrimaryKey(roleId);
            //删除角色-权限
            rolePermissionMapper.deleteByRoleId(roleId);
            //删除用户-角色
            roleUserMapper.deleteByRoleId(roleId);
            return ResultDTO.okOf();
        }

        return ResultDTO.errorOf(CustomizeErrorCode.INCORRECT_INFORMATION);
    }

    @Override
    public ResultDTO getRoleByUserId(Integer userId) {
        List<SysRoleUser> byUserId = roleUserMapper.getByUserId(userId);
        if (byUserId!=null&& byUserId.size()>0){
            SysRoleUser roleUser = byUserId.get(0);
            SysRole role = roleMapper.selectByPrimaryKey(roleUser.getRoleId());
            return ResultDTO.okOf(role);
        }
        return ResultDTO.errorOf(CustomizeErrorCode.INCORRECT_INFORMATION);
    }

    @Override
    public SysRole getRoleById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }



}
